package com.sinoglobal.dumping.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.GridView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.adapter.Dumpling_ReceivedCardAdapter;
import com.sinoglobal.dumping.api.Dumpling_RemoteImpl;
import com.sinoglobal.dumping.base.Dumpling_SinoAsyncTask;
import com.sinoglobal.dumping.base.Dumpling_SinoBaseActivity;
import com.sinoglobal.dumping.bean.Dumpling_ReceivedCardVo;

import java.util.ArrayList;

/**
 * 我的贺卡
 */
public class Dumpling_ReceivedCardActivity extends Dumpling_SinoBaseActivity implements AdapterView.OnItemClickListener {

    private GridView gridCardList;
    private int itemNum;
    private Dumpling_ReceivedCardAdapter cardAdapter;
    private ViewStub noDataView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dumpling_activity_received_card);
        mTemplateTitleText.setText("我的贺卡");
        View view = getView(R.id.title_right_btn_share);
        hideView(mTemplateRightText, mTemplateRightImg, view);
        noDataView = (ViewStub) findViewById(R.id.dumpling_make_card_nodata);
        itemNum = getIntent().getIntExtra("cardNumber", 10);
        gridCardList = (GridView) findViewById(R.id.dumpling_received_card_list);
        if (itemNum>0){
            getListInfo();
        }else {
            noDataView.inflate();
        }
        gridCardList.setOnItemClickListener(this);
    }

    private void getListInfo() {
        new Dumpling_SinoAsyncTask<Void, Void, ArrayList<Dumpling_ReceivedCardVo>>(Dumpling_ReceivedCardActivity.this) {
            @Override
            public ArrayList<Dumpling_ReceivedCardVo> before(Void... params) throws Exception {
                return Dumpling_RemoteImpl.getInstance().myCard(itemNum);
            }

            @Override
            public void after(ArrayList<Dumpling_ReceivedCardVo> dumpling_cardVos) {
                if (dumpling_cardVos != null && dumpling_cardVos.size() > 0){
                    cardAdapter = new Dumpling_ReceivedCardAdapter(dumpling_cardVos, Dumpling_ReceivedCardActivity.this);
                    gridCardList.setAdapter(cardAdapter);
                }else {
                    noDataView.inflate();
                }

            }

            @Override
            public void exception() {
                noDataView.inflate();
            }
        }.execute();
    }

    @Override
    public void onAntiViolence(View view) {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (!TextUtils.isEmpty(cardAdapter.getCardList().get(i).getDumplingUserPutcardId())) {
            Intent intent = new Intent(this, Dumpling_MyCardDetailActivity.class);
            intent.putExtra("DumplingUserPutcardId", cardAdapter.getCardList().get(i).getDumplingUserPutcardId());
            intent.putExtra("fromType", 1);
            startActivity(intent);
        }else {
            showToast("参数错误");
        }
    }
}
